<?php

App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController
{

    public $layout = 'imprenta';
    public $uses = array('User','Sucursale');
    //public $components =

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }

    public function index()
    {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null)
    {
        $this->User->id = $id;
        if (!$this->User->exists())
        {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add()
    {
        if ($this->request->is('post'))
        {
            //debug($this->request->data);exit;
            $this->User->create();
            $this->request->data['User']['estado']=1;
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
        $dsucursal = $this->Sucursale->find('list', array('fields'=>'Sucursale.nombre'));
        $this->set(compact('dsucursal'));
        //debug($dsucursal);exit;
        
        $usuario = array('admin' => 'Admin', 'auxiliar' => 'Auxiliar','auxiliar2'=>'Auxiliar Administrador');
        $estado = array('0'=>'Alta','1'=>'Baja');
        $this->set(compact('usuario','estado'));
        //debug($estado);exit;
        
    }

    public function edit($id = null)
    {
        $this->User->id = $id;
        if (!$this->User->exists())
        {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put'))
        {
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
        else
        {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $dsucursal = $this->Sucursale->find('list', array('fields'=>'Sucursale.nombre'));
        $this->set(compact('dsucursal'));
        //debug($dsucursal);exit;
        
        $usuario = array('admin' => 'Admin', 'auxiliar' => 'Auxiliar','auxiliar2'=>'Auxiliar Administrador');
        //$estado = array('0'=>'Alta','1'=>'Baja');
        $this->set(compact('usuario'));
        //debug($estado);exit;
    }
    
    public function cambiopass($id = null)
    {
        $this->User->id = $id;
        if (!$id) {
            $this->Session->setFlash('No existe tal registro');
            $this->redirect(array('action' => 'index'), null, true);
        }
        if (empty($this->data)) {
            $this->data = $this->User->read();
        } else {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('Los datos fueron modificados');
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('no se pudo modificar!!');
            }
        }
    }

    public function eliminar($id = null)
    {
        if (!$id) {
            $this->Session->setFlash('id Invalida para borrar');
            $this->redirect(array('action' => 'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash('El Usuario  ' . $id . ' fue borrado');
            $this->redirect(array('action' => 'index'));
        }
    }
    function baja($id = null)
    {
        $this->User->id = $id;
        if (!$id) 
        {
            $this->Session->setFlash('No se pudo dar de baja');
            $this->redirect(array('action' => 'index'), null, true);
        }
            $estado = 0;
            $this->request->data['User']['estado'] = $estado;
        
        if ($this->User->save($this->data)) 
        {
            $this->Session->setFlash('Los datos fueron modificados');
            $this->redirect(array('action' => 'index'), null, true);
        } 
        else 
        {
            $this->Session->setFlash('no se pudo modificar!!');
        }
     }
    function alta($id = null)
    {
        $this->User->id = $id;
        if (!$id) 
        {
            $this->Session->setFlash('No se pudo dar de baja');
            $this->redirect(array('action' => 'index'), null, true);
        }
        $estado = 1;
        $this->request->data['User']['estado'] = $estado;
            
        if ($this->User->save($this->data)) 
        {
            $this->Session->setFlash('Los datos fueron modificados');
            $this->redirect(array('action' => 'index'), null, true);
        } 
        else 
        {
            $this->Session->setFlash('no se pudo modificar!!');
        }
    }

    public function login()
    {
       $this->layout = 'admin';
    
    if ($this->request->is('post'))
     {
        if ($this->Auth->login())
        {
            if($this->Auth->user('role')=='admin')
            {
                $this->redirect(array('controller'=>'principal', 'action'=>'index'));            
            }
            else
            {
                $this->redirect(array('controller'=>'principal', 'action'=>'index'));
            }       
        }
            else
            {
                $this->Session->setFlash(__('Usuario o Password invalidos'));
            }
        
     }
    }

    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }
}
